package com.attilax.office;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.attilax.cmd.CmdX;
import com.attilax.io.pathx;
import com.attilax.text.ParamUtil;

public class OfficeTaskServlet extends HttpServlet {
	
	public static void main(String[] args) {
		System.out.println("0--");
	}
	public static final Logger logger = LoggerFactory.getLogger(" OfficeTaskServlet");

	private int getFilePage(String filepath, String type_id) {
		String bat_dir = pathx.appPathV2();

		String cmd = "office_page_cli.bat @f@  " + type_id;
		cmd = cmd.replace("@f@", filepath);
		logger.info(" cmdR612a:" + cmd);
		String ret_from_cli_console = CmdX.exe(cmd);
		logger.info("------------------------------------------------------------>>>>>>>>>>>>>>>>>>cmd_ret_from_bat_console:" + ret_from_cli_console);
		logger.info("------------------------------------<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//cmd_ret_from_bat_console_end" );
		String page_frm_parse_console_ret_int = "";
		try {
			page_frm_parse_console_ret_int = ParamUtil.getParam(ret_from_cli_console);
		} catch (StringIndexOutOfBoundsException e) { // retry mode
			logger.warn("-------  cmd_ret_from_bat_console sec time,cmd:" + cmd);
			ret_from_cli_console = CmdX.exe(cmd);
			try {
				page_frm_parse_console_ret_int = ParamUtil.getParam(ret_from_cli_console);
			} catch (Exception e2) {
				logger.info("---------------------------------------------->>>>>>>>>>>>>>  cmd_ret_from_bat_console:" + ret_from_cli_console);
				logger.info("----------------------------<<<<<<<<<<<<<<<//cmd_ret_from_bat_console_end");
				throw new RuntimeException("ParamUtil.getParam err ,  cmd_ret_from_bat_console:" + ret_from_cli_console,
						e2);
			}

		}

		logger.info(" /*****{page from bat console:" + page_frm_parse_console_ret_int);
		logger.info(" ******/}page from bat console end");

		try {
			return Integer.parseInt(page_frm_parse_console_ret_int);
		} catch (NumberFormatException e) {
			throw new RuntimeException(page_frm_parse_console_ret_int);
		}

	}

	private String getFilepath(HttpServletRequest req) {
		if (req.getParameter("f") != null)
			return req.getParameter("f");
		if (req.getParameter("p") != null)
			return req.getParameter("p");
		throw new RuntimeException("no param f or p:getFilepath");
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		PrintWriter out = resp.getWriter();
		PrintWriter pw = out;

		// ati print pages
		String filepath;
		String type_id;
		try {
			filepath = getFilepath(req);
			type_id = getTypeid(req);
		} catch (Exception e) {
			if (e.getMessage().startsWith("no param f or p"))
				out.println("0");
			out.flush();
			return;
		}

		if (new File(filepath).exists()) {
			try {
				filepath = filepath.replace("/", "\\");
				// -------------------------------
				int wordPageCounts = getFilePage(filepath, type_id);

				pw.println(wordPageCounts);
			} catch (Exception e) {
				pw.println(JSON.toJSONString(e, true));
			} finally {
				// closeSocket();
			}

		} else {
			pw.println("0");// 文件不存在
		}
		pw.flush();
		// out.close();

	}

	private String getTypeid(HttpServletRequest req) {
		if (req.getParameter("f") != null)
			return "word";
		if (req.getParameter("p") != null)
			return "ppt";
		throw new RuntimeException("no param f or p:getTypeid");
	}

}
